/**
 * Copyright (c) 2019-2021,  陈俊兵 (cjb6b.com@gmail.com)
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ktjiaoyu.thread.schedule;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *
 * @packageName com.ktjiaoyu.mianshi.thread
 * @fileName SleepWaitDiffTest.java
 * @author 陈俊兵 (cjb6b.com@gmail.com)
 * @createTime 2020/9/29 15:17
 * @lastModify 2020/9/29 15:17
 * @version 1.0.0
 */
public class SleepWaitDiffTest {

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class Product{
        // 商品ID
        private String productId;
        // 商品名称
        private String productName;
        // 商品库存
        private int count;
    }

    public static final Product product1 = new Product("1000", "电视机",100);
    public static final Product product2 = new Product("1001", "电冰箱",50);

    public static void main(String[] args) {


        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                Product orderProduct = product1;
                String pId = orderProduct.getProductId().intern(); // 把字符串常量池中的字符串地址给pId
                synchronized (pId){
                    System.out.println(Thread.currentThread().getName() + "给" + orderProduct.getProductName() + "减库存");
                    int count = orderProduct.getCount();
                    try {
                        // Thread.sleep不会释放锁，wait会释放
                        //Thread.sleep(1000);
                        // sleep是Thread的静态方法， wait是Object的方法，会释放当前锁定的对象。调用前必须锁住该对象
                        // wait会释放锁
                        pId.wait(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    orderProduct.setCount(count - 1);
                    System.out.println(Thread.currentThread().getName() +"，查看商品" + orderProduct.getProductName() + "库存变为" + orderProduct.getCount());
                }
            }
        },"t1");

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                Product orderProduct = product1;
                String pId = orderProduct.getProductId().intern(); // 把字符串常量池中的字符串地址给pId
                synchronized (pId){
                    int count = orderProduct.getCount();
                    System.out.println(Thread.currentThread().getName() + "给" + orderProduct.getProductName() + "减库存");
                    // 减库存
                    orderProduct.setCount(count - 1);
                    System.out.println(Thread.currentThread().getName() +"，查看商品" + orderProduct.getProductName() + "库存变为" + orderProduct.getCount());
                }
            }
        },"t2");
        t1.start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        t2.start();
    }
}
